<template>
  <div class="production-management">
    <page>
      <div slot="body">
        <div>
          <div class="line"></div>
          <!-- <div style="font-size: 15px; font-weight: 600">产品总生产目标</div>
          <el-form class="form" label-width="140px" inline>
            <el-form-item class="form-item" label="设计总需求量：" prop="name">
              <el-input v-model="total" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(1)"></i>
            </el-form-item>
            <el-form-item class="form-item" label="设计完成时间：" prop="name">
              <el-input v-model="time" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(2)"></i>
            </el-form-item>
          </el-form> -->
          <div style="text-align: right;">
            <el-radio-group v-model="value1" @input="radioInput">
              <el-radio-button v-for="(item, index) in list1" :label="item.code" :key="index">{{item.name}}</el-radio-button>
            </el-radio-group>
          </div>
        </div>
        <div style="margin-bottom:  10px;">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd"> 添加产品检验 </el-button>
        </div>
        <el-table :data="tableData" border>
          <el-table-column label="序号" type="index" width="50px"></el-table-column>
          <el-table-column label="生产批号" prop="batchCode"></el-table-column>
          <el-table-column label="产品类型" prop="prodType">
            <template slot-scope="scope">
              <span>{{
                (list1.find(item => item.code == scope.row.prodType) || {}).name ||
                scope.row.prodType
              }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弦长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弦高" prop="height"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="半径" prop="radius"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="实测-弧长" prop="realArcLength"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="实测-弦高" prop="realHeight"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="实测-半径" prop="realRadius"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="工字钢规格" prop="steel"></el-table-column>

          <el-table-column v-if="value1=='wp'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="实测-长" prop="realArcLength"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="实测-宽" prop="realProdWide"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="钢筋规格" prop="rebarSpecifications"></el-table-column>

          <el-table-column v-if="value1=='ljg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="厚" prop="proThick"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="实测-长" prop="realArcLength"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="实测-宽" prop="realProdWide"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="实测-厚" prop="realProThick"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="钢板规格" prop="steelPlateSpecifications"></el-table-column>

          <el-table-column v-if="value1=='xdg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="止浆段长度" prop="prodLong"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="实测-长" prop="realArcLength"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="实测-止浆段长度" prop="realProdLong"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="小导管规格" prop="conduitSpecifications"></el-table-column>
          <el-table-column label="检验日期" prop="checkTime"></el-table-column>
          <el-table-column label="检验数量" prop="finishNum"></el-table-column>
          <el-table-column label="检验是否合格" prop="conduitSpecifications">
            <template slot-scope="scope">
              <span v-if="scope.row.pass == 0">合格</span>
              <span v-if="scope.row.pass == 1">不合格</span>
              </template>
          </el-table-column>
          <el-table-column label="检验图片" prop="conduitSpecifications">
            <template slot-scope="scope">
              <el-image 
                style="width: 40px; height: 40px"
                :src="scope.row.image" 
                :preview-src-list="[scope.row.image]">
              </el-image>
            </template>
          </el-table-column>
          <!-- <el-table-column label="规格" prop="tunnelArea">
            <template slot-scope="scope">
              <span>{{
                (statusOptions.find(item => item.dictValue == scope.row.unit) || {}).dictLabel || scope.row.tunnelArea
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单元" prop="unit">
            <template slot-scope="scope">
              <span>{{
                (unitList.find(item => item.dictValue == scope.row.unit) || {}).dictLabel || scope.row.unit
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="大样图" prop="image" show-overflow-tooltip></el-table-column> -->
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <!-- <el-link type="primary" @click="handleEdit(scope.row)" :underline="false">编辑</el-link> -->
              <el-link type="primary" @click="handleDelete(scope.row)" :underline="false">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
          <el-form-item label="设计总需求量：" prop="prodNum" v-if="num == 1">
            <el-input v-model="form.prodNum" placeholder="设计总需求量：" />
          </el-form-item>
          <el-form-item label="设计完成时间：" prop="finishDate" v-if="num == 2">
            <el-date-picker v-model="form.finishDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="prodTitle" :close-on-click-modal="false" :visible.sync="openProd" width="1600px" append-to-body>
        <el-table :data="tableData1" @selection-change="handleSelectionChange" ref="multipleTable" border>
          <el-table-column label="生产批次号" prop='batchCode'></el-table-column>
          <el-table-column label="产品编码" prop="prodCode"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弦长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弦高" prop="height"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="半径" prop="radius"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="工字钢规格" prop="steel"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="厚" prop="proThick"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="钢板规格" prop="steelPlateSpecifications"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="钢筋规格" prop="rebarSpecifications"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="止浆段长度" prop="prodLong"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="小导管规格" prop="conduitSpecifications"></el-table-column>
          <el-table-column label="生产数量" align="center" prop="finishNum"/>
          
          <!-- <el-table-column label="生产时间" align="center" prop="madeDate" width="180" /> -->
          <el-table-column label="操作员" prop="operator"></el-table-column>
          <el-table-column align="center" type="selection"></el-table-column>

        </el-table>
        <el-form ref="prodForm" :model="prodForm" style="margin-top: 20px;" :rules="prodRules" label-width="140px">
          <!-- <el-form-item v-if="value1=='ggj'" label="实测-弧长：" prop="realArcLength">
            <el-input v-model="prodForm.realArcLength" type="number" placeholder="请输入弧长" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="实测-弦高：" prop="realHeight">
            <el-input v-model="prodForm.realHeight" type="number" placeholder="请输入弦高" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="实测-半径：" prop="radius">
            <el-input v-model="prodForm.radius" type="number" placeholder="请输入半径" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="实测-长：" prop="realArcLength">
            <el-input v-model="prodForm.realArcLength" type="number" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="实测-宽：" prop="realProdWide">
            <el-input v-model="prodForm.realProdWide" type="number" placeholder="请输入宽" />
          </el-form-item>
          
          <el-form-item v-if="value1=='ljg'" label="实测-长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="实测-宽：" prop="realProdWide">
            <el-input v-model="prodForm.realProdWide" placeholder="请输入宽" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="实测-厚：" prop="realProThick">
            <el-input v-model="prodForm.realProThick" placeholder="请输入厚" />
          </el-form-item>
          
          <el-form-item v-if="value1=='xdg'" label="实测-长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" type="number" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="实测-止浆段长度：" prop="realProdLong">
            <el-input v-model="prodForm.realProdLong" placeholder="请输入止浆段长度" />
          </el-form-item> -->
          <el-form-item label="检验是否合格：" prop="operator">
            <el-select v-model="prodForm.pass" placeholder="请选检验是否合格">
              <el-option label="合格" :value="0" />
              <el-option label="不合格" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="检测时间" prop="dateRange">
            <el-date-picker v-model="prodForm.checkTime" type="date" value-format="yyyy-MM-dd"
              placeholder="检测时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="操作员：" prop="operator">
            <el-input v-model="prodForm.operator" placeholder="请输入止浆段长度" />
          </el-form-item>
          <el-form-item label="检验图片：">
            <ImageUpload ref="ImageRef" :limit="1"></ImageUpload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitProdForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer">
        <el-pagination
          background
          :page-size="pageVO.pageSize"
          :current-page.sync="pageVO.pageNum"
          :total="pageVO.total"
          :layout="pageOptions.layout"
          :page-sizes="pageOptions.pageSizes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </page>
  </div>
</template>

<script>
import {  addProdPlan, getList, getDicts, addCheck, deleteProd,getplanList,getprolist } from '@/api/prodCheck';
import { getToken } from '@/utils/auth';
import pageQuery from '../../mixins/pageQuery';
import ImageUpload from "@/components/ImageUpload/index.vue";
export default {
  mixins: [pageQuery],
  components: { ImageUpload },
  data() {
    return {
      form: {
        prodNum: '',
        finishDate: '',
        id: '',
      },
      list1: [],
      value1: 'ggj',
      prodForm: {},
      open: false,
      openProd: false,
      total: '',
      time: '',
      title: '',
      prodTitle: '',
      tableData: [],
      areaList: [],
      statusOptions: [],
      unitList: [],
      num: 0,
      rules: {
        prodNum: [{ required: true, message: '设计总需求量不能为空', trigger: 'blur' }],
        finishDate: [{ required: true, message: '设计完成时间不能为空', trigger: 'blur' }],
      },
      prodRules: {
        tunnel: [{ required: true, message: '请输入所属隧道', trigger: 'blur' }],
        tunnelArea: [{ required: true, message: '请选择隧道区域', trigger: 'chage' }],
        steel: [{ required: true, message: '请输入工字钢类型', trigger: 'blur' }],
        unit: [{ required: true, message: '请选择单元', trigger: 'chage' }],
        prodCode: [{ required: true, message: '请输入产品编号', trigger: 'blur' }],
        // upload:[
        //   { required: true, message: "", trigger: "chage" }
        // ]
      },
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/steel-arch/client/file/upload', // 上传的图片服务器地址
      headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      fileList: [],
      tableData1:[]
    };
  },
  created() {
    this.getplan();
    this.getTableData();
    const { projectId } = this.$store.getters;
    this.prodForm.projectId = projectId;
    getDicts({ dictType: 'sys_tunnel_area' }).then(res => {
      this.statusOptions = res.data.data;
    });
    getDicts({ dictType: 'sys_prod_unit' }).then(res => {
      this.unitList = res.data.data;
    });
    this.getprolist()
  },
  methods: {
    getprolist() {
      getprolist().then(res =>{
        if(res.success == true){
          this.list1 = res.data
        }
      })
    },
    getplan() {
      const { projectId } = this.$store.getters;
      // getProdPlan(projectId).then(res => {
      //   this.total = res.data.prodNum;
      //   this.time = res.data.finishDate;
      //   (this.form.prodNum = res.data.prodNum), (this.form.finishDate = res.data.finishDate);
      //   this.form.id = res.data.id;
      // });
    },
    radioInput(e){
      this.getTableData()
    },
    getTableData() {
      const { projectId } = this.$store.getters;
      const parmas = {
        projectId: projectId,
        prodType: this.value1,
      };
      getList(parmas).then(res => {
        this.tableData = res.data;
        this.pageVO.total = res.data.length
      });
    },
    edit(num) {
      this.open = true;
      if (num == 1) {
        this.title = '设计总需求量修改';
        this.num = 1;
      } else {
        this.title = '设计完成时间修改';
        this.num = 2;
      }
    },
    submitForm: function () {
      
      this.$refs['form'].validate(valid => {
        if (valid) {
          const { projectId } = this.$store.getters;
          this.form.projectId = projectId;
          this.form.prodType = this.value1
          
          addProdPlan(this.form).then(response => {
            this.$message.success('修改成功');
            this.open = false;
            this.getplan();
            this.getTableData();
          });
        }
      });
    },
    submitProdForm: function () {
      if (!this.checkedSelection) {
        return this.$message.success('请选择类型');
      }
      this.$refs['prodForm'].validate(valid => {
        if (valid) {
          this.prodForm.batchCode  = this.checkedSelection.batchCode;
          this.prodForm.prodType = this.value1;
          this.prodForm.finishNum = this.checkedSelection.finishNum
          this.prodForm.image = this.$refs.ImageRef.getUrl();
          addCheck(this.prodForm).then(response => {
              this.$message.success('新增成功');
              this.openProd = false;
              this.getTableData();
            })
        }
      });
    },
    cancel() {
      this.open = false;
      this.$refs.prodForm.resetFields();
      this.openProd = false;
    },
    handleAdd() {
      this.openProd = true;
      this.prodTitle = '添加产品检验';
      this.prodForm={}
      this.prodForm.image = ''
      
      // this.$nextTick(() => {this.$refs.EnterRef.setUrl("");})
      let parm ={
        prodType:this.value1,
      }
      getplanList(parm).then(res => {
        this.tableData1 = res.data.data;
      });
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`只允许上传单个文件`);
    },
    // 上传失败
    handleUploadError(err) {
      this.$message.error('上传失败, 请重试');
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      this.$message.success('上传成功');
      this.prodForm.image = res.data;
    },
    // 删除文件
    // handleDelete(index) {
    //   //this.fileList.splice(index, 1);
    // },
    handleEdit(row) {
      this.openProd = true;
      this.prodForm = JSON.parse(JSON.stringify(row));
      this.prodTitle='编辑产品';
      const { projectId } = this.$store.getters;
      this.prodForm.projectId = projectId;
      console.log(this.$refs.EnterRef)
      // this.fileList.push({url:this.prodForm.image})
      // this.$nextTick(() => {this.$refs.EnterRef.setUrl(this.prodForm.image);})
      
    },
    handleDelete(row) {
      this.$confirm('是否确认删除产品ID为"' + row.prodType + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return deleteProd(row.id);
        })
        .then(() => {
          this.getTableData();
          this.$message.success('删除成功');
        });
    },
    handleSelectionChange(val) {

// 单选
if (val.length == 1) {
  let item = val[val.length - 1];
  this.checkedSelection = JSON.parse(JSON.stringify(item));
}

// 单选选中多条时，需要清空所选数据
if (val.length > 1) {
  this.$refs.multipleTable.clearSelection(); // 清空选项
  this.$refs.multipleTable.toggleRowSelection(val.pop()); // 选中最后点击的数据
}

// 取消选中
if (val.length == 0) {
  this.checkedSelection = {};
}
},
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: flex-start;
  margin: 20px 0;
}

.line {
  width: 8px;
  background: #1890ff;
  margin-right: 15px;
}
.form {
  display: flex;
  margin-top: 10px;
  ::v-deep {
    .el-form-item__label {
      font-weight: normal;
    }
  }
}
.form-item {
  position: relative;
}
.el-icon-edit {
  font-size: 20px;
  right: 10px;
  position: absolute;
  top: 8px;
}
</style>
